var app = getApp();

Page({

  data: {
    isShow: true
  },

  onLoad: function (e) {
    // 加载网络数据，获取地址列表
    // console.log('fanhui;e');
    if (e.addrtype){
      this.setData({
        addrtype: e.addrtype
      })
    }
  },
  init1: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    })
    var openId = app.globalData.openId;
    console.log(openId, app.globalData.openId);
    wx.request({
      url: app.globalData.url + 'addr_list?openid=' + openId,
      success: function (res) {
        var addList = res.data;
        console.log(openId, addList)

        if (addList == null || addList == '') {
          that.setData({
            isShow: false
          })

          wx.showModal({
            content: '您还没有收获地址，点击新建',
            confirmText: '新建',
            confirmColor: '#E83517',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/mine/shouhuo/shouhuo?etype=0',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else { //有地址时显示地址
          that.setData({
            addList: addList,
            isShow: true
          })
        }
      },
      complete:function(){
        wx.hideLoading()
      }
    })
  },
  // 选择地址
  selectAddr(e){
    var that = this
    var o = e.currentTarget.dataset
    wx.request({
      url: app.globalData.url+ 'addr_list_limit?add_id='+o.id+'&openid='+ wx.getStorageSync('openId'),
      success:res=>{
        if (that.data.addrtype){
          wx.navigateBack({
            delta:1
          })
        }
        that.init1();
      }
    })
  },
  onShow: function (e) {
    this.init1();
  },

  //添加地址
  add: function (e) {
    wx.navigateTo({
      url: '/pages/mine/shouhuo/shouhuo?etype=0',
    })
  },
  //编辑地址
  edit: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/mine/shouhuo/shouhuo?etype=1&id=' + id,
    })
  },
  //删除地址
  del: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var openId = wx.getStorageSync('openId')
    var addList = that.data.addList;
    wx.request({
      url: app.globalData.url + 'addr_del?openid=' + openId + '&id=' + id,
      success: function (res) {
        console.log(res);
        if (res.errMsg == "request:ok") {
         wx.showToast({
           title: '删除成功',
           icon: 'success',
           duration: 2000,
           mask: true,
         })

          that.init1();
         
        }
      }
    })
  }

})